package com.xzw.cameratest.utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 23930 on 2017/8/3.
 */
public class AndroidJsonUtils {

    public static JSONObject parseObject(String jsonStr) {
        JSONObject obj = null;
        try {
            obj  = new JSONObject(jsonStr);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return obj;
    }

    public static JSONArray parseArray(String jsonStr) {
        JSONArray array = null;
        try {
            array  = new JSONArray(jsonStr);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return array;
    }

    public static  JSONObject getSubObject(JSONObject root, String nodeName) {
        JSONObject obj = null;
        try {
            obj = root.getJSONObject(nodeName);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return obj;
    }

    public static  JSONArray getSubArray(JSONObject root, String nodeName) {
        JSONArray array = null;
        try {
            array = root.getJSONArray(nodeName);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return array;
    }

    public static  JSONObject getSubObject(JSONArray array, int index) {
        JSONObject obj = null;
        try {
            obj = array.getJSONObject(index);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return obj;
    }
}
